﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Invent.Plugins;
using DatabaseInterface;

namespace Invent
{
    public partial class frmMainForm : Form
    {
        //static Thread splashThread = null;
        Settings setup;
        private DBInterface db;
        bool IsLogin = false;

       
        public frmMainForm()
        {
            InitializeComponent();
            lblStatus.Text = "Статус БД: Соединение не установлено";
            IsLogin = false;
            //Чтение настроек из файла
            setup = new Settings();
            setup.ReadFromFile();
            this.Text = setup.Title+" Версия "+setup.Version + " - Business Framework";
         }

    

            
        private void btnLogin_Click(object sender, EventArgs e)
        {
            frmLoginForm login = new frmLoginForm();
            login.ShowDialog();
            if (login.strLogin != "")
            {
                try
                {
                    db = new DBInterface(DBInterface.DataBases.MySQL, setup.Server, setup.Database,
                        login.strLogin, login.strPassword);
                    if (db.State)
                        lblStatus.Text = "Connected ver "+ db.Version ;
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
           
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (IsLogin)
            {
                if (db != null)
                {
                    frmProjectsForm prjForm = new frmProjectsForm(db, true);
                    prjForm.MdiParent = this;
                    prjForm.Show();
                }
            }
            else 
                MessageBox.Show("Нет соединения с БД", "Ошибка",MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mnuSetup_Click(object sender, EventArgs e)
        {
            frmSetupForm setup = new frmSetupForm();
            setup.StartPosition = FormStartPosition.CenterParent;
            setup.ShowDialog(); 
        }

        private void mnuAboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAboutForm about = new frmAboutForm();
            about.StartPosition = FormStartPosition.CenterParent;
            about.ShowDialog();
        }

        private void frmMainForm_Load(object sender, EventArgs e)
        {
            //Получить список плагинов
            Global.Plugins.FindPlugins(Application.StartupPath + @"\Plugins");

            // загрузка плагинов в меню
            foreach (Invent.Plugins.Types.AvailablePlugin pluginOn in Global.Plugins.AvailablePlugins)
            {
                mnuPlugins.DropDownItems.Add(pluginOn.Instance.Name);
            }
        }
    }
}
